package H4102;

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import java.awt.GridLayout;

import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import sourceCode.Aeroport;
import sourceCode.Bagage;
import sourceCode.Chariot;


public class Onglets extends JPanel {

    @SuppressWarnings("compatibility:1117199462804902954")
    private static final long serialVersionUID = 4376297500934154000L;

    private String[] columnNames = { "ID", "ID conteneur", "Type conteneur" };

    private MyJTableModel jTableModelBagages = new MyJTableModel();
    private MyJTableModel jTableModelChariots = new MyJTableModel();


    private JTabbedPane tabbedPane = new JTabbedPane();
    private JScrollPane scrollPaneBagages;
    private JScrollPane scrollPaneChariots;
    private JTable jTableBagages;
    private JTable jTableChariots;


    private ImageIcon iconB = createImageIcon("img/Bagage.jpg");
    private ImageIcon iconC = createImageIcon("img/Chariot.jpg");

    protected ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public Onglets() {
        super(new GridLayout(1, 1));
        this.jTableModelBagages.setColumnsNames(this.columnNames);
        this.jTableModelChariots.setColumnsNames(this.columnNames);

    }

    public void Init(Aeroport aeroport) {
        MajDataBagages(aeroport);
        MajDataChariots(aeroport);

        this.jTableBagages = new JTable(this.jTableModelBagages);
        this.jTableChariots = new JTable(this.jTableModelChariots);

        this.scrollPaneBagages = new JScrollPane(this.jTableBagages);
        this.jTableBagages.setFillsViewportHeight(true);

        this.scrollPaneChariots = new JScrollPane(this.jTableChariots);
        this.jTableChariots.setFillsViewportHeight(true);


        if (tabbedPane.getTabCount() == 0) {
            this.tabbedPane.addTab("Bagages ", this.iconB, this.scrollPaneBagages, "Voir les bagages");

            this.tabbedPane.addTab("Chariots ", this.iconC, this.scrollPaneChariots, "Voir les chariots");

            //Add the tabbed pane to this panel.
            add(this.tabbedPane);

            //The following line enables to use scrolling tabs.
            this.tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        }

    }


    public void MajDataBagages(Aeroport aeroport) {
        ArrayList<Bagage> bagages = aeroport.getListeBagages();
        Object[][] res = { };

        if (bagages != null) {
            int compteurBagage = 0;
            int compteurColonne = 0;

            res = new Object[bagages.size()][3];

            for (Bagage b : bagages) {
                res[compteurBagage][compteurColonne] = b.getId();
                if (b.getConteneur() != null) {
                    res[compteurBagage][compteurColonne + 1] = b.getConteneur().getId();
                    res[compteurBagage][compteurColonne + 2] =
                            b.getConteneur().getClass().getName().split("sourceCode.")[1];
                } else {
                    res[compteurBagage][compteurColonne + 1] = b.getChariot().getId();
                    res[compteurBagage][compteurColonne + 2] =
                            b.getChariot().getClass().getName().split("sourceCode.")[1];
                }
                compteurBagage++;
            }

        }
        this.jTableModelBagages.setData(res);
        if (this.scrollPaneBagages != null)
            this.scrollPaneBagages.updateUI();
    }

    public void MajDataChariots(Aeroport aeroport) {
        ArrayList<Chariot> chariots = aeroport.getListeChariots();

        Object[][] res = { };
        if (chariots != null) {
            int compteurChariot = 0;
            int compteurColonne = 0;

            res = new Object[chariots.size()][3];

            for (Chariot c : chariots) {
                res[compteurChariot][compteurColonne] = c.getId();
                if (c.getNoeud() != null) {
                    res[compteurChariot][compteurColonne + 1] = c.getNoeud().getId();
                    res[compteurChariot][compteurColonne + 2] =
                            c.getNoeud().getClass().getName().split("sourceCode.")[1];
                } else {
                    res[compteurChariot][compteurColonne + 1] = c.getRail().getId();
                    res[compteurChariot][compteurColonne + 2] =
                            c.getRail().getClass().getName().split("sourceCode.")[1];
                }
                compteurChariot++;
            }
        }
        this.jTableModelChariots.setData(res);
        if (this.scrollPaneChariots != null)
            this.scrollPaneChariots.updateUI();
    }

    public JTable getJTableBagages() {
        return this.jTableBagages;
    }

    public JTable getJTableChariots() {
        return this.jTableChariots;
    }

    public JScrollPane getScrollPaneBagages() {
        return scrollPaneBagages;
    }


    protected class MyJTableModel extends AbstractTableModel {

        @SuppressWarnings("compatibility:8706238514378055901")
        private static final long serialVersionUID = -7088955385820839792L;

        private String[] columnNames;
        private Object[][] data;

        public int getColumnCount() {
            if (this.columnNames != null)
                return this.columnNames.length;
            return 0;
        }

        public int getRowCount() {
            if (this.columnNames != null)
                return this.data.length;
            return 0;
        }

        public String getColumnName(int col) {
            return this.columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return this.data[row][col];
        }

        public void setColumnsNames(String[] columnNames) {
            this.columnNames = columnNames;
        }

        public void setData(Object[][] data) {
            this.data = data;
        }

    }

}
